package com.gitee.huanminabc.jcommon.exception;

import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;

/**
 * @author huanmin
 * @date 2024/2/2
 */

@Slf4j
public class ExceptionUtil {
    //将堆栈转换为字符串
    public  static String  getStackTrace(Throwable e){
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        e.printStackTrace(new java.io.PrintWriter(buf, true));
        return buf.toString();
    }
    //将堆栈转换为字符串
    public  static String  getStackTrace(Throwable e,String message, Object... args){
        //将{}替换为%s
        String format = String.format(message.replaceAll("\\{\\s*}", "%s"), args);
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        e.printStackTrace(new java.io.PrintWriter(buf, true));
        return format+ buf;
    }
}
